package com.chenfan.magic.service.proxy;

import com.chenfan.common.vo.Response;
import com.chenfan.magic.annation.FetchHandle;
import com.chenfan.magic.base.R;
import com.chenfan.magic.feign.PrivilegeServer;
import com.chenfan.privilege.request.SUserVOReq;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author liuming
 * @date 2021-06-07 14:26
 */
@Service
public class DcControlExportTotalListProxy {


    @Autowired
    private PrivilegeServer privilegeServer;

    public void handle(Object keys) {
        if (keys instanceof R) {
            R result = (R) keys;
            Map map = result.getData();
            ArrayList<Map> arrayList = (ArrayList<Map>) map.get("result");
            //{jobNo=585491848772878336, userId=568869188605476864, realName=456456, template_name=财务-结算-发出商品明细, status=2, row_qty=763, user_name=王旭, job_number=1888, job_statr_time=2021-06-04 15:35:43.0, qty=0}
            List<Long> userIds = arrayList.stream().map(x -> Long.valueOf((String) x.get("userId"))).collect(Collectors.toList());
            SUserVOReq userVoReq = new SUserVOReq();
            userVoReq.setUserIds(userIds);
            Response response = privilegeServer.listUsers(userVoReq);
            //[{account=wangxu, accountName=, bankName=, bankNumber=, companyCode=, companyId=5, createName=, createTime=null, departmentCode=, departmentId=19, departmentName=, devFlag=null, email=, isDeleted=false, loginTime=null, mobilePhone=, officePhone=, positionName=, pwdUpdateTime=null, remark=, roleCodes=e8191290788f4138b9842c52ad294a74, roleIdList=1395216107819765761, roleName=数据中心测试, status=null, updateName=, updateTime=null, userCode=016f05177c4043bc91b083d7eb001845, userId=1367752669531213825, userSys=1, username=王旭}]
            ArrayList<Map> userList = (ArrayList<Map>) response.getObj();
            Map userMap = userList.stream()
                    .collect(Collectors.toMap(new Function<Map, Object>() {
                                                  @Override
                                                  public String apply(Map map) {
                                                      return (String) map.get("userId");
                                                  }
                                              },
                            x -> x));
            for (Map mp : arrayList) {
                String userId = (String) mp.get("userId");
                Map user = (Map) userMap.get(userId);
                if (CollectionUtils.isEmpty(user)) {
                    continue;
                }
                mp.put("user_name", user.get("username"));
                mp.put("job_number", user.get("jobNumber"));
            }
            map.put("result", arrayList);
        }
    }
}
